#include "wifi_device.h"
#include "wifi_adapter.h"
#include "string.h"
#include "los_task.h"
#include "los_compiler.h"
#include "cmsis_os2.h"
#include "ohos_init.h"
#include "ohos_types.h"

#include "stddef.h"
#include "stdio.h"
#include "stm32f4xx_ll_gpio.h"
#include "iot_uart.h"
#include "wifi_scan_info.h"
#include "wifi_adapter.h"
#include "wifi_device.h"
#include "stdlib.h"
#include "string.h"
#include "delay.h"

WifiEvent g_testWifiEvent;

/** Connection state change */
static void OnWifiConnectionChanged(int state, WifiLinkedInfo *info)
{
    char ip_str[16]={0};
    if(info != NULL){
        int_to_ip(ip_str, info->ipAddress);
    }
    if(state == WIFI_STATE_NOT_AVAILABLE){
        printf("Wi-Fi Disconnect\n");
    }else{
        printf("Wi-Fi Connected, ip is : %s\n", ip_str);
    }
}
/** Scan state change */
static void OnWifiScanStateChanged(int state, int size)
{
    //(WifiScanInfo *)malloc(sizeof(WifiScanInfo)*size);
    if(state == WIFI_STATE_AVAILABLE)
    {
        printf("Wi-Fi Scan OK, get %d Hotspot\n", size);
        // PrintScan();
        // 申请内存
        WifiScanInfo info;
        unsigned int isize;
        // 获取扫描队列
        if(GetScanInfoList(&info, &isize)==WIFI_SUCCESS){
            PrintScanInfo(&info, isize); 
        }
    }
    else
    {
        printf("Wi-Fi Scan Failed\n");
    }
}
/** Hotspot state change */
static void OnHotspotStateChanged(int state)
{
    if(state == WIFI_STATE_AVAILABLE)
    {
        printf("Hotspot Enable\n");
    }
    else
    {
        printf("Hotspot Disable\n");
    }
}
/** Station connected */
static void OnHotspotStaJoin(StationInfo *info)
{
    char mac[18] = {0};
    char ip[16] = {0};
    mac_hex2str(mac, (char *)(info->macAddress));
    int_to_ip(ip, info->ipAddress);
    printf("%s Join! IP is %s\n", mac, ip);
}
/** Station disconnected */
static void OnHotspotStaLeave(StationInfo *info)
{
    char mac[18] = {0};
    char ip[16] = {0};
    mac_hex2str(mac, (char *)(info->macAddress));
    int_to_ip(ip, info->ipAddress);
    printf("%s Leave! IP is %s\n", mac, ip);
}

void wifi_entry()
{
    printf("----------Wi-Fi Test Start----------\n");
    // WifiErrorCode res;
    if(WifiUartInit() != WIFI_SUCCESS){
        printf("%s line %d error : Init wifi error...\n", __func__, __LINE__);
        return;
    }
    if(WifiReset() != WIFI_SUCCESS){
        return;
    }
    // 给event内部的函数指针赋值
    g_testWifiEvent.OnWifiScanStateChanged = OnWifiScanStateChanged;
    g_testWifiEvent.OnWifiConnectionChanged = OnWifiConnectionChanged;
    g_testWifiEvent.OnHotspotStaJoin = OnHotspotStaJoin;
    g_testWifiEvent.OnHotspotStaLeave = OnHotspotStaLeave;
    g_testWifiEvent.OnHotspotStateChanged = OnHotspotStateChanged;
    RegisterWifiEvent(&g_testWifiEvent);
    if(EnableWifi() != WIFI_SUCCESS){
        return;
    }
    delay_ms(1000);
    Scan();
    WifiDeviceConfig config;
    memset(config.ssid, 0, WIFI_MAX_SSID_LEN);
    memset(config.preSharedKey, 0, WIFI_MAX_KEY_LEN);
    memcpy(config.ssid, WIFI_SSID, strlen(WIFI_SSID));
    memcpy(config.preSharedKey, WIFI_PASSWD, strlen(WIFI_PASSWD));
    int net_id;
    if(AddDeviceConfig(&config, &net_id) != WIFI_SUCCESS){
        return;
    }
    ConnectTo(net_id);
}

void wifi_task()
{
    UINT32 uwRet = -1;
    UINT32 taskID = 0;
    TSK_INIT_PARAM_S stTask = {0};
    stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)wifi_entry;
    stTask.uwStackSize = 0x400*5;
    stTask.pcName = "wifi task";
    stTask.usTaskPrio = 10;
    uwRet = LOS_TaskCreate(&taskID, &stTask);
    if (uwRet != LOS_OK) {
        printf("Task fs create failed\n");
    }
}